/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package socket.chat;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author atsi
 */
public class ChatClientGUI {

//Globals
    private static ChatClient chatClient;
    public static String userName = "Anonymous";
//Gui Globals Main Window
    public static JFrame mainWindow = new JFrame();
    private static JButton B_ABOUT = new JButton();
    private static JButton B_CONNECT = new JButton();
    private static JButton B_DISCONNECT = new JButton();
    private static JButton B_HELP = new JButton();
    private static JButton B_SEND = new JButton();
    private static JLabel L_Message = new JLabel("Message: ");
    public static JTextField TF_Message = new JTextField(20);
    private static JLabel L_Conversation = new JLabel();
    public static JTextArea TA_CONVERSATION = new JTextArea();
    private static JScrollPane SP_CONVERSATION = new JScrollPane();
    public static JList JL_ONLINE = new JList();
    private static JLabel L_ONLINE = new JLabel();
    private static JScrollPane SP_ONLINE = new JScrollPane();
    private static JLabel L_LoggedInAs = new JLabel();
    private static JLabel L_LoggedInAsBox = new JLabel();
//GUI Globals-Login Window
    public static JFrame LogInWindow = new JFrame();
    public static JTextField TF_UserNameBox = new JTextField(20);
    private static JButton B_ENTER = new JButton("ENTER");
    private static JLabel L_EnterUserName = new JLabel("Enter Username: ");
    private static JPanel P_Login = new JPanel();
    private static String HOST;

    public ChatClientGUI(String host) {
        HOST = host;
    }

    public static void main(String args[]) {
        ChatClientGUI chatClientGUI = new ChatClientGUI("localhost");
        chatClientGUI.init();
    }

    public void init() {
        BuildMainWindow();
        Initialize();
    }

    public static void Connect() {

        try {
            final int PORT = 444;
            Socket SOCK = new Socket(HOST, PORT);
            System.out.println("You connected to: " + HOST);

            chatClient = new ChatClient(SOCK);

            //Send name to add to "Online" list
            PrintWriter OUT = new PrintWriter(SOCK.getOutputStream());
            OUT.println(userName);
            OUT.flush();

            Thread X = new Thread(chatClient);
            X.start();
        } catch (Exception X) {
            System.out.print(X);
            JOptionPane.showMessageDialog(null, "Server not responding. ");
            System.exit(0);
        }
    }

    public static void Initialize() {
        B_SEND.setEnabled(false);
        B_DISCONNECT.setEnabled(false);
        B_CONNECT.setEnabled(true);
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
        }
        BuildLogInWindow();
    }

    public static void BuildLogInWindow() {
        LogInWindow.setTitle("What's your name?");
        LogInWindow.setSize(400, 100);
        LogInWindow.setLocation(250, 200);
        LogInWindow.setResizable(false);

        P_Login.add(L_EnterUserName);
        P_Login.add(TF_UserNameBox);
        P_Login.add(B_ENTER);
        LogInWindow.add(P_Login);

        Login_Action();
        LogInWindow.setVisible(true);
    }

    public static void BuildMainWindow() {
        mainWindow.setTitle(userName + "'s Chat Box");
        mainWindow.setSize(450, 500);
        mainWindow.setLocation(620, 150);
        mainWindow.setResizable(false);
        ConfigureMainWindow();
        MainWindow_Action();
        mainWindow.setVisible(true);
    }

    public static void ConfigureMainWindow() {
        mainWindow.setBackground(new Color(255, 255, 255));
        mainWindow.setSize(500, 320);
        mainWindow.getContentPane().setLayout(null);

        B_SEND.setBackground(new Color(0, 0, 255));
        B_SEND.setForeground(new Color(255, 255, 255));
        B_SEND.setText("SEND");
        mainWindow.getContentPane().add(B_SEND);
        B_SEND.setBounds(250, 40, 81, 25);

        B_DISCONNECT.setBackground(new Color(0, 0, 255));
        B_DISCONNECT.setForeground(new Color(255, 255, 255));
        B_DISCONNECT.setText("DISCONNECT");
        mainWindow.getContentPane().add(B_DISCONNECT);
        B_DISCONNECT.setBounds(10, 40, 110, 25);

        B_CONNECT.setBackground(new Color(0, 0, 255));
        B_CONNECT.setForeground(new Color(255, 255, 255));
        B_CONNECT.setText("CONNECT");
        B_CONNECT.setToolTipText("");
        mainWindow.getContentPane().add(B_CONNECT);
        B_CONNECT.setBounds(130, 40, 110, 25);
//
//        B_HELP.setBackground(new Color(0, 0, 255));
//        B_HELP.setForeground(new Color(255, 255, 255));
//        B_HELP.setText("HELP");
//        mainWindow.getContentPane().add(B_HELP);
//        B_HELP.setBounds(420, 40, 70, 25);
//
//        B_ABOUT.setBackground(new Color(0, 0, 255));
//        B_ABOUT.setForeground(new Color(255, 255, 255));
//        B_ABOUT.setText("ABOUT");
//        mainWindow.getContentPane().add(B_ABOUT);
//        B_ABOUT.setBounds(340, 40, 75, 25);

        L_Message.setText("Message:");
        mainWindow.getContentPane().add(L_Message);
        L_Message.setBounds(10, 10, 60, 20);

        TF_Message.requestFocus();
        TF_Message.setForeground(new Color(0, 0, 0));
        mainWindow.getContentPane().add(TF_Message);
        TF_Message.setBounds(70, 4, 260, 30);

        L_Conversation.setHorizontalAlignment(SwingConstants.CENTER);
        L_Conversation.setText("Convertation:");
        mainWindow.getContentPane().add(L_Conversation);
        L_Conversation.setBounds(100, 70, 140, 16);

        TA_CONVERSATION.setColumns(20);
        TA_CONVERSATION.setFont(new Font("Tahoma", 0, 12));
        TA_CONVERSATION.setForeground(new Color(0, 0, 255));
        TA_CONVERSATION.setLineWrap(true);
        TA_CONVERSATION.setRows(5);
        TA_CONVERSATION.setEditable(false);

        SP_CONVERSATION.setHorizontalScrollBarPolicy(
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        SP_CONVERSATION.setVerticalScrollBarPolicy(
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        SP_CONVERSATION.setViewportView(TA_CONVERSATION);
        mainWindow.getContentPane().add(SP_CONVERSATION);
        SP_CONVERSATION.setBounds(10, 90, 330, 180);

        L_ONLINE.setHorizontalAlignment(SwingConstants.CENTER);
        L_ONLINE.setText("Currently Online:");
        L_ONLINE.setToolTipText("");
        mainWindow.getContentPane().add(L_ONLINE);
        L_ONLINE.setBounds(350, 70, 130, 16);

        //String[] TestNames = new String[]{"boib"};
        JL_ONLINE.setForeground(new Color(0, 0, 255));
        //JL_ONLINE.setListData(TestNames);

        SP_ONLINE.setHorizontalScrollBarPolicy(
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        SP_ONLINE.setVerticalScrollBarPolicy(
                ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        SP_ONLINE.setViewportView(JL_ONLINE);
        mainWindow.getContentPane().add(SP_ONLINE);
        SP_ONLINE.setBounds(350, 90, 130, 180);

        L_LoggedInAs.setFont(new Font("Tahoma", 0, 12));
        L_LoggedInAs.setText("Currently Logged In As");
        mainWindow.getContentPane().add(L_LoggedInAs);
        L_LoggedInAs.setBounds(348, 0, 140, 15);

        L_LoggedInAsBox.setHorizontalAlignment(SwingConstants.CENTER);
        L_LoggedInAsBox.setFont(new Font("Tahoma", 0, 12));
        L_LoggedInAsBox.setForeground(new Color(255, 0, 0));
        L_LoggedInAsBox.setBorder(
                BorderFactory.createLineBorder(new Color(0, 0, 0)));
        mainWindow.getContentPane().add(L_LoggedInAsBox);
        L_LoggedInAsBox.setBounds(340, 17, 150, 20);
    }

    public static void Login_Action() {
        B_ENTER.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                ACTION_B_ENTER();
            }
        });
    }

    public static void ACTION_B_ENTER() {
        if (!TF_UserNameBox.getText().equals("")) {
            userName = TF_UserNameBox.getText().trim();
            L_LoggedInAsBox.setText(userName);
            ChatServer.ConnectedUsers.add(userName);
            mainWindow.setTitle(userName + "'s Chat Box");
            LogInWindow.setVisible(false);
            B_SEND.setEnabled(true);
            B_DISCONNECT.setEnabled(true);
            B_CONNECT.setEnabled(false);
            Connect();
        } else {
            JOptionPane.showMessageDialog(null, "Please enter a name:");
        }
    }

    public static void MainWindow_Action() {
        B_SEND.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                ACTION_B_SEND();
            }
        });

        B_DISCONNECT.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                ACTION_B_DISCONNECT();
            }
        });
        B_CONNECT.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                BuildLogInWindow();

            }
        });
//        B_HELP.addActionListener(new ActionListener() {
//
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                ACTION_B_HELP();
//            }
//        });
//        B_ABOUT.addActionListener(new ActionListener() {
//
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                ACTION_B_HELP();
//            }
//        });
    }

    public static void ACTION_B_SEND() {
        if (!TF_Message.getText().equals("")) {
            chatClient.Send(TF_Message.getText());
            TF_Message.requestFocus();
        }
    }

    public static void ACTION_B_DISCONNECT() {
        try {
            chatClient.Disconnect();
        } catch (IOException ex) {
            Logger.getLogger(ChatClientGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
//    public static void ACTION_B_HELP() {
//        JOptionPane.showMessageDialog(null, "MSC NETWORK-CENTRIC SYSTEMS 2012!");
//    }
}
